/*
 * BanBu_SoundSetting
 * @description 本文件提供信息发送信息接收提示音界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供信息发送信息接收提示音界面的ViewController
 */

#import "BanBu_SoundSetting.h"
#import "AppDataManager.h"
#import "BanBu_NavButton.h"
#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"
#import "BanBu_MainCell.h"
@interface BanBu_SoundSetting ()

@end

@implementation BanBu_SoundSetting

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (self.isReceive)
    {
        self.title = NSLocalizedString(@"soundSwitch", nil);
    }
    else
    {
        self.title = NSLocalizedString(@"sendMsgBoolKey", nil);
    }
    
    self.tableView.backgroundView = nil;
    self.tableView.backgroundColor = [UIColor colorWithRed:255.0/255 green:250.0/255 blue:240.0/255 alpha:1.0];
    
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable]autorelease];
    self.tableView.backgroundView = img;
    
//    BanBu_NavButton *btn_return = [[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeBack];
//    btn_return.position = ButtonPositionLeft;
//    btn_return.frame=CGRectMake(0, 0,50, 30);
//    [btn_return addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
//
//    [btn_return setTitle:NSLocalizedString(@"returnButton", nil) forState:UIControlStateNormal];
//    btn_return.titleLabel.font = [UIFont systemFontOfSize:14];
//    UIBarButtonItem *cancelItem = [[[UIBarButtonItem alloc] initWithCustomView:btn_return] autorelease];
//    self.navigationItem.leftBarButtonItem = cancelItem;
//    [btn_return release];

    if (self.isReceive)
    {
        if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"MusicSwith"]length])
        {
            _sectionNumber = 2;
            NSArray *soundArr = [NSArray arrayWithObjects:@"msg_1",@"msg_2",@"msg_3",@"msg_4",@"msg_5",@"msg_6",@"msg_7",@"msg_8",@"msg_9", nil];
            MyAppDataManager.musicName = [[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"MusicSwith"];
            for(int i=0; i<soundArr.count; i++)
            {
                if([[soundArr objectAtIndex:i] isEqualToString:MyAppDataManager.musicName])
                {
                    _selectRow = i;
                    break;
                }
            }
        }
        else
        {
            _sectionNumber = 1;
        }
    }
    else
    {
        if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"sendMsgBoolKey"]length])
        {
            _sectionNumber = 2;
            NSArray *soundArr = [NSArray arrayWithObjects:@"send_0",@"send_1",@"send_2",@"send_3",@"send_4",@"send_5",@"send_6", nil];
            
            MyAppDataManager.musicName = [[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"sendMsgBoolKey"];
            
            for(int i=0; i<soundArr.count; i++)
            {
                if([[soundArr objectAtIndex:i] isEqualToString:MyAppDataManager.musicName])
                {
                    _selectRow = i;
                    break;
                }
            }
        }
        else
        {
            _sectionNumber = 1;
        }
    }
}
-(void)popself
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     // Return the number of sections.
    return _sectionNumber;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(!section)
    {
        return 1;
    }
    if (self.isReceive)
    {
        return 9;
    }
    else
    {
        return 7;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    BanBu_MainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!cell)
    {
        cell = [[[BanBu_MainCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }

    cell.textLabel.backgroundColor = [UIColor clearColor];
    if([ThemeManager getThemeImage:@"tablecell_backimage.png"])
        cell.backgroundColor = [UIColor colorWithPatternImage:[ThemeManager getThemeImage:@"tablecell_backimage.png"]];
    
    if(!indexPath.section)
    {
        //接收信息提示音
        if (self.isReceive)
        {
            cell.textLabel.text = NSLocalizedString(@"soundSwitch", nil);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            //一个switch没必要这么做
            UISwitch *aSwitch = (UISwitch *)[cell viewWithTag:100];
            if(!aSwitch)
            {
                aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(310-80, 7, 80, 30)];
                aSwitch.tag = 100;
                [aSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = aSwitch;
                [aSwitch release];
            }
            
            _soundSwitch = aSwitch;
            if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"MusicSwith"]length])
            {
                _soundSwitch.on = YES;
            }
            else
            {
                _soundSwitch.on = NO;
            }
        }
        //发送信息提示音
        else
        {
            cell.textLabel.text = NSLocalizedString(@"sendMsgBoolKey", nil);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            //一个switch没必要这么做
            UISwitch *aSwitch = (UISwitch *)[cell viewWithTag:100];
            if(!aSwitch)
            {
                aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(310-80, 7, 80, 30)];
                aSwitch.tag = 100;
                [aSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = aSwitch;
                [aSwitch release];
            }
            
            _soundSwitch = aSwitch;
            if([[[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"] valueForKey:@"sendMsgBoolKey"]length])
            {
                _soundSwitch.on = YES;
            }
            else
            {
                _soundSwitch.on = NO;
            }
        }
    }
    else
    {
        cell.accessoryView = nil;
        if (indexPath.row == _selectRow)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
    }
    
    return cell;
}
 
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    backView.backgroundColor = [UIColor clearColor];
    UILabel *headerTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 300, 30)];
    headerTitle.backgroundColor = [UIColor clearColor];

    headerTitle.font = [UIFont systemFontOfSize:14];
    headerTitle.textColor = [UIColor darkGrayColor];
    if(!section)
    {
        if (self.isReceive)
        {
            headerTitle.text = NSLocalizedString(@"newmsgNotice", nil);
        }
        else
        {
            headerTitle.text = NSLocalizedString(@"sendMsgBoolKey", nil);
        }

    }
    else
    {
        headerTitle.text = NSLocalizedString(@"soundType", nil);
    }
    [backView addSubview:headerTitle];
    [headerTitle release];
    return [backView autorelease];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section)
    {
        NSMutableDictionary *settingsDic = [NSMutableDictionary dictionaryWithDictionary:[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]];
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        BanBu_MainCell *cell = (BanBu_MainCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectRow inSection:1]];
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        cell = (BanBu_MainCell *)[tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        _selectRow = indexPath.row;
        
        NSString * musicName = nil;
        if (self.isReceive)//接消息设置
        {
            NSArray *soundArr = [NSArray arrayWithObjects:@"msg_1",@"msg_2",@"msg_3",@"msg_4",@"msg_5",@"msg_6",@"msg_7",@"msg_8",@"msg_9", nil];
            MyAppDataManager.musicName = [soundArr objectAtIndex:_selectRow];
            musicName = MyAppDataManager.musicName;
            
            [settingsDic setValue:MyAppDataManager.musicName forKey:@"MusicSwith"];
        }
        else//发消息设置
        {
            NSArray *soundArr = [NSArray arrayWithObjects:@"send_0",@"send_1",@"send_2",@"send_3",@"send_4",@"send_5",@"send_6", nil];
            MyAppDataManager.sendMsgMusicName = [soundArr objectAtIndex:_selectRow];
            musicName = MyAppDataManager.sendMsgMusicName;
            
            [settingsDic setValue:MyAppDataManager.sendMsgMusicName forKey:@"sendMsgBoolKey"];
        }


        NSMutableDictionary *settingsUpdata = [NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
        [settingsUpdata setValue:settingsDic forKey:@"settings"];
        [UserDefaults setValue:settingsUpdata forKey:MyAppDataManager.useruid];
        [self.tableView reloadData];
        
        
        //播放音乐
        if(_player)
        {
            [_player release],_player = nil;
        }
        
        NSString *path=[[NSBundle mainBundle]pathForResource:musicName ofType:@"mp3"];
        
        _player = [[AVAudioPlayer alloc]initWithData:[NSData dataWithContentsOfFile:path] error:nil];
        _player.volume = 1.0;
        [_player prepareToPlay];
        [_player play];
    }
}

-(void)switchAction:(UISwitch *)sender
{
    NSMutableDictionary *settingsDic = [NSMutableDictionary dictionaryWithDictionary:[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"settings"]];

    if (self.isReceive)//接消息提示音
    {
        if(sender.on)
        {
            _sectionNumber = 2;
            _soundSwitch.on = YES;
            if([MyAppDataManager.musicName isEqualToString:@""])
            {
                NSArray *soundArr = [NSArray arrayWithObjects:@"msg_1",@"msg_2",@"msg_3",@"msg_4",@"msg_5",@"msg_6",@"msg_7",@"msg_8",@"msg_9", nil];
                
                MyAppDataManager.musicName = [soundArr objectAtIndex:_selectRow];
            }
            [settingsDic setValue:MyAppDataManager.musicName forKey:@"MusicSwith"];
        }
        else
        {
            _sectionNumber = 1;
            _soundSwitch.on = NO;
            [settingsDic setValue:@"" forKey:@"MusicSwith"];
            MyAppDataManager.musicName = @"";
        }
    }
    else//发消息提示音
    {
        if(sender.on)
        {
            _sectionNumber = 2;
            _soundSwitch.on = YES;
            if([MyAppDataManager.sendMsgMusicName isEqualToString:@""])
            {
                NSArray *soundArr = [NSArray arrayWithObjects:@"send_0",@"send_1",@"send_2",@"send_3",@"send_4",@"send_5",@"send_6", nil];
                
                MyAppDataManager.sendMsgMusicName = [soundArr objectAtIndex:_selectRow];
            }
            [settingsDic setValue:MyAppDataManager.sendMsgMusicName forKey:@"sendMsgBoolKey"];
        }
        else
        {
            _sectionNumber = 1;
            _soundSwitch.on = NO;
            [settingsDic setValue:@"" forKey:@"sendMsgBoolKey"];
            MyAppDataManager.sendMsgMusicName = @"";
        }
    }
    if (_openOrCloseCallBack)
    {
        _openOrCloseCallBack(_soundSwitch.isOn);
    }
    NSMutableDictionary *settingsUpdata = [NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
    [settingsUpdata setValue:settingsDic forKey:@"settings"];
    [UserDefaults setValue:settingsUpdata forKey:MyAppDataManager.useruid];
    [self.tableView reloadData];
}


-(void)dealloc
{
    self.openOrCloseCallBack = nil;
    [_player release],_player = nil;
    [super dealloc];
}


@end
